package hk.ust.cse.comp3111.studentSchedule;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Fragment;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class ViewCourse extends Fragment {
	
    
	RegisteredClassDatabaseHandler db_register;
	CourseDatabaseHandler db_course;
	Spinner courseSelectSpinner;
	TextView courseCode_viewByCourse;
	TextView courseName_viewByCourse;
	GridView grid_viewByCourse;
	ArrayAdapter<String> adapter;
	
	public ViewCourse(){}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.view_course, container, false);

        
        return rootView;
    }
	
	
	
	
	@Override
	public void onStart() {
		super.onStart();
		courseSelectSpinner = (Spinner) this.getView().findViewById(R.id.courseSelectSpinner);
	
		courseCode_viewByCourse=(TextView) this.getView().findViewById(R.id.courseCode_viewByCourse);
		courseName_viewByCourse=(TextView) this.getView().findViewById(R.id.courseName_viewByCourse);
		grid_viewByCourse=(GridView) this.getView().findViewById(R.id.grid_viewByCourse);
		
		db_register= new RegisteredClassDatabaseHandler(this.getActivity());
		List<String> registeredCourse = new ArrayList<String>();
		registeredCourse = db_register.getAllCourseCode();
		ArrayList<String> listItems=new ArrayList<String>();
		
		for(String s : registeredCourse){
        	listItems.add(s);
        }
		

		
		adapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item,registeredCourse);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		courseSelectSpinner.setAdapter(adapter);
		
		courseSelectSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {


			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				refresh(courseSelectSpinner.getItemAtPosition(arg2).toString());
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
	    });
		
		
		
		
		

		
	
	}
	

	public void refresh(String selected){
		db_course = new CourseDatabaseHandler(this.getActivity());
		Course c = new Course();
		
		
		courseCode_viewByCourse.setText(selected);
		courseName_viewByCourse.setText(db_course.getCourseName(selected));
		courseCode_viewByCourse.setTextColor(Color.parseColor("#6c00d7"));
		courseName_viewByCourse.setTextColor(Color.parseColor("#6c00d7"));
		
		List<Integer> list_classNo_registered = new ArrayList<Integer>();
		ArrayList<String> items=new ArrayList<String>();
		
		list_classNo_registered = db_register.getAllRegisteredClassNumber(selected);
		

		
		for (Integer s : list_classNo_registered) {
			c=db_course.getCourseByClassNo(s);
			items.add(c.getSection());
			
			if(c.getRep2_weekDay()==0){
				//one rep only
				items.add(c.getWeekDayInWords().get(0)+" "+c.milisToStringArray(c.getRep1_fromTime())[0]+":"+c.milisToStringArray(c.getRep1_fromTime())[1]
						+" - "+c.milisToStringArray(c.getRep1_toTime())[0]+":"+c.milisToStringArray(c.getRep1_toTime())[1]
						
						);
			}
			
			else if (c.getRep3_weekDay()==0){
				//two rep
				items.add(c.getWeekDayInWords().get(0)+" "+c.milisToStringArray(c.getRep1_fromTime())[0]+":"+c.milisToStringArray(c.getRep1_fromTime())[1]
						+" - "+c.milisToStringArray(c.getRep1_toTime())[0]+":"+c.milisToStringArray(c.getRep1_toTime())[1]
								
						+"\n"+c.getWeekDayInWords().get(1)+" "+c.milisToStringArray(c.getRep2_fromTime())[0]+":"+c.milisToStringArray(c.getRep2_fromTime())[1]
								+" - "+c.milisToStringArray(c.getRep2_toTime())[0]+":"+c.milisToStringArray(c.getRep2_toTime())[1]
						
						);
			}
			
			else {
				// three rep
				items.add(c.getWeekDayInWords().get(0)+" "+c.milisToStringArray(c.getRep1_fromTime())[0]+":"+c.milisToStringArray(c.getRep1_fromTime())[1]
						+" - "+c.milisToStringArray(c.getRep1_toTime())[0]+":"+c.milisToStringArray(c.getRep1_toTime())[1]
						
						+"\n"+c.getWeekDayInWords().get(1)+" "+c.milisToStringArray(c.getRep2_fromTime())[0]+":"+c.milisToStringArray(c.getRep2_fromTime())[1]
								+" - "+c.milisToStringArray(c.getRep2_toTime())[0]+":"+c.milisToStringArray(c.getRep2_toTime())[1]
						
						+"\n"+c.getWeekDayInWords().get(2)+" "+c.milisToStringArray(c.getRep3_fromTime())[0]+":"+c.milisToStringArray(c.getRep3_fromTime())[1]
								+" - "+c.milisToStringArray(c.getRep3_toTime())[0]+":"+c.milisToStringArray(c.getRep3_toTime())[1]
						
						);
				
			
			}
		}
		
		/*items.add("");
		items.add("");
		
		Cursor cursor = this.getActivity().getContentResolver()
	            .query(
	                    Uri.parse("content://com.android.calendar/events"),
	                    new String[] { "calendar_id", "title", "description",
	                            "dtstart", "dtend", "eventLocation" }, null,
	                    null, null);
	    
	    if (cursor.moveToFirst()) {
            do {
            	if(cursor.getString(1).contains(selected)&&!cursor.getString(2).contains("Added by Student Schedule")){
            		items.add(object)
            	}

            } while (cursor.moveToNext());
        }*/
	    
	    
		
		
		
		adapter=new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_list_item_1,items);
		grid_viewByCourse.setAdapter(adapter);
	}
	
	
	
	
}
